using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;

namespace LogAnalyzer.Globalization
{
    internal class ResourceManager
    {

        public delegate void CultureChangedHandler(CultureInfo newCulture);

        #region static

        private static ResourceManager _current = null;
        private static object _generatingCurrent = new object();
        private static CultureInfo _ciSet = null;

        public static void SetCulture(int id)
        {
            ChangeCulture(new CultureInfo(id));
        }
        public static void SetCulture(string name)
        {
            ChangeCulture(new CultureInfo(name));
        }
        private static void ChangeCulture(CultureInfo ci)
        {
            if (_ciSet != null && ci.LCID == _ciSet.LCID)
                return;

            _ciSet = ci;
            _current = null;
            if (CultureChanged != null)
            {
                CultureChanged(ci);
            }
        }

        public static ResourceManager Current
        {
            get
            {
                if (_current == null)
                {
                    lock (_generatingCurrent)
                    {
                        if (_current == null)
                        {
                            _current = new ResourceManager(_ciSet);
                        }
                    }
                }

                return _current;
            }
        }
                
        public static event CultureChangedHandler CultureChanged;

        #endregion


        private CultureInfo _ci = null;
        
        public ResourceManager(CultureInfo ci)
        {
            if (ci == null)
                _ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
            else
                _ci = ci;
        }

        public CultureInfo Culture
        {
            get 
            {
                return _ci;
            }
        }

        public string GetString(string resourceName)
        {
            return GetString(resourceName, new object[0]);
        }
        public string GetString(string resourceName, params object[] args)
        {
            if (args == null)
                args = new object[0];

            string resource = Resources.ResourceManager.GetString(resourceName, _ci);


            if (resource != null)
            {
                if (_ci.IsNeutralCulture)
                    return string.Format(CultureInfo.InvariantCulture, resource, args);
                else
                    return string.Format(_ci, resource, args);
            }
            else
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    string res= "[RES:" + resourceName;

                    if (_ci.IsNeutralCulture)
                        foreach (object arg in args)
                            res += string.Format(CultureInfo.InvariantCulture, ",{0}", new object[] { arg });
                    else
                        foreach (object arg in args)
                            res += string.Format(_ci, ",{0}", new object[] { arg });

                    res += "]";

                    return res;
                }
                else
                {
                    return null;
                }
            }
        }

    }
}
